﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IMark.DAO
{
    public class HinhSanPhamModels
    {
        public static bool Add(List<HINH_SAN_PHAM> lstImage)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    foreach (var item in lstImage)
                    {
                        context.HINH_SAN_PHAM.Add(item);
                    }
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public static bool Delete(long ID,string urlRoot)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    HINH_SAN_PHAM hinhSanPham = context.HINH_SAN_PHAM.Find(ID);
                    Utils.DeleteFile(urlRoot + hinhSanPham.Hinh);
                    context.HINH_SAN_PHAM.Remove(hinhSanPham);
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
        public static List<HINH_SAN_PHAM> GetAll()
        {
            using (var context = new EMartEntities())
            {
                return context.HINH_SAN_PHAM.ToList();
            }
        }
        public static HINH_SAN_PHAM GetByID(int ID)
        {
            using (var context = new EMartEntities())
            {
                return context.HINH_SAN_PHAM.Find(ID);
            }
        }
        public static List<HINH_SAN_PHAM> GetByIDProduct(long IDSanPham)
        {
            using (var context = new EMartEntities())
            {
                return context.HINH_SAN_PHAM.Where(u=>u.IDSanPham == IDSanPham).ToList();
            }
        }
        public static HINH_SAN_PHAM GetTop1ByIDProduct(long IDSanPham)
        {
            using (var context = new EMartEntities())
            {
                return context.HINH_SAN_PHAM.Where(u => u.IDSanPham == IDSanPham).FirstOrDefault();
            }
        }
        public static int GetQuatityImage(long IDSanPham)
        {
            using (var context = new EMartEntities())
            {
                return context.HINH_SAN_PHAM.Where(u => u.IDSanPham == IDSanPham).ToList().Count();
            }
        }
    }
}